package com.xy.pattern.visitor;

public class Computer {
    ComputerPart cpu = new Cpu();
    ComputerPart memery = new Memery();

    public void accept(Visitor visitor) {
        this.cpu.accept(visitor);
        this.memery.accept(visitor);
    }

    public static void main(String[] args) {
        PersonVisitor personVisitor = new PersonVisitor();
        new Computer().accept(personVisitor);
        System.out.println(personVisitor.totalPrice);
    }

}

abstract class ComputerPart {
    abstract void accept(Visitor v);
    abstract double getPrice();
}

class Cpu extends ComputerPart {
    @Override
    void accept(Visitor v) {
        v.visitCpu(this);
    }

    @Override
    double getPrice() {
        return 500;
    }
}

class Memery extends ComputerPart {
    @Override
    void accept(Visitor v) {
        v.visitMemery(this);
    }

    @Override
    double getPrice() {
        return 600;
    }
}

interface Visitor {
    void visitCpu(Cpu cpu);
    void visitMemery(Memery memery);
}

class PersonVisitor implements Visitor {

    double totalPrice = 0.0;
    @Override
    public void visitCpu(Cpu cpu) {
        totalPrice += cpu.getPrice() * 0.6;
    }

    @Override
    public void visitMemery(Memery memery) {
        totalPrice += memery.getPrice() * 0.7;
    }
}

class CorpVisitor implements Visitor {
    double totalPrice = 0.0;
    @Override
    public void visitCpu(Cpu cpu) {
        totalPrice += cpu.getPrice() * 0.6;
    }

    @Override
    public void visitMemery(Memery memery) {
        totalPrice += memery.getPrice() * 0.7;
    }
}


